import Vue from 'vue'
import VueRouter from 'vue-router'

import { getToken } from '@/utils/token'

const Register = () => import('../views/Register')// 登录
const Login = () => import('../views/Login')// 注册
const Layout = () => import('../views/Layout') // 首页
const Article = () => import('../views/Article') // 面经
const Collect = () => import('../views/Collect') // 收藏
const Like = () => import('../views/Like')// 喜欢
const User = () => import('../views/User') // 我的
const ArticleDetail = () => import('../views/ArticleDetail')// 封装的token
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/Article',
      children: [
        {
          path: '/Article', component: Article
        },
        {
          path: '/Collect', component: Collect
        },
        {
          path: '/Like', component: Like
        },
        {
          path: '/User', component: User
        }
      ]
    },
    {
      path: '/detail/:id', component: ArticleDetail
    },
    {
      path: '/register', component: Register
    },
    {
      path: '/login', component: Login
    }
  ]
})
const wihtList = ['/login', '/register']
router.beforeEach((to, form, next) => {
  if (getToken() || wihtList.includes(to.path)) {
    next()
  } else {
    next('/login')
    // this.$router.push('/login')
  }
})
export default router
